package com.niuwa.p2p.service.impl.redenvelope;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.PropertyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.niuwa.p2p.core.mvc.entity.PageBean;
import com.niuwa.p2p.core.utils.AppConstants;
import com.niuwa.p2p.dao.dictionary.DictionaryDao;
import com.niuwa.p2p.dao.redenvelope.RedEnvelopeDao;
import com.niuwa.p2p.entity.Dictionary;
import com.niuwa.p2p.entity.PageQuery;
import com.niuwa.p2p.entity.redenvelope.RedEnvelope;
import com.niuwa.p2p.service.redenvelope.IRedEnvelopeService;
import com.niuwa.p2p.utils.JsonToHashMap;
import com.niuwa.p2p.vo.front.account.RedEnvelopeVo;
import com.niuwa.p2p.vo.front.product.FrontRedEnvelopeVo;

@Service
public class RedenvelopeServiceImpl implements IRedEnvelopeService{
	
	@Autowired
	private RedEnvelopeDao redEnvelopeDao;
	
	@Autowired
	private DictionaryDao dictionaryDao;
	
	@Override
	public List<RedEnvelopeVo> selectRedEnvelopeBySelectiveListPage(
			Map<String, Object> paramMap) {
		return redEnvelopeDao.selectRedEnvelopeBySelectiveListPage(paramMap);
	}

	@Override
	public RedEnvelope selectByExchangeNo(String exchangeNo) {
		return redEnvelopeDao.selectByPrimaryKey(exchangeNo);
	}

	@Override
	public int bindRedEnvelopeCust(RedEnvelope record) {
		return redEnvelopeDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public Map<String, Object> selectRedEnvelopeCountBySelective(
			Map<String, Object> paramMap) {
		return redEnvelopeDao.selectRedEnvelopeCountBySelective(paramMap);
	}

	@Override
	public Map<String, List<Dictionary>> getRedEnvelopeDict() throws Exception {
		Map<String, List<Dictionary>> map=new HashMap<String, List<Dictionary>>();
		//红包使用范围
		List<Dictionary> redUseRangeDict=dictionaryDao.getDictByDictType(AppConstants.RED_USE_RANGE);
		map.put("redUseRangeDict", redUseRangeDict);
		//红包使用状态
		List<Dictionary> redUseStatDict=dictionaryDao.getDictByDictType(AppConstants.RED_USE_STATUS);
		map.put("redUseStatDict", redUseStatDict);
		return map;
	}

	
	@Override
	public List<FrontRedEnvelopeVo> selectAllRedEnve4Customer(Map<String, Object> paramMap) {
		List<FrontRedEnvelopeVo> list = redEnvelopeDao.selectAllRedEnve4Customer(paramMap);
		return list;
	}

	/**
	 * 查找红包列表
	 * param paramMap
	 * return PageBean<RedEnvelope>
	 */
	@Override
	public PageBean<RedEnvelope> searchRedEnvelopeList(Map<String, Object> paramMap) {
		assert paramMap.get(PageQuery.PAGEQUERY) != null;
		//总条数
		int totalCount = redEnvelopeDao.searchRedEnvelopeListCountByCondition(paramMap);
		PageBean<RedEnvelope> page = new PageBean<RedEnvelope>(totalCount);
		// 当前页
		page.setPage(((PageQuery) paramMap.get(PageQuery.PAGEQUERY)).getPage());
		page.setRows(redEnvelopeDao.searchRedEnvelopeListByCondition(paramMap));
		return page;
		
	}

	
	/**
     * 查询所有待审核红包不分页
     * @param paramMap
     * @return
     */
	@Override
	public List<RedEnvelope> searchAllRedEnvelopeList(Map<String, Object> paramMap) {
		return redEnvelopeDao.searchRedEnvelopeListByCondition(paramMap);
	}

	
	/**
     * 一键审核或一键拒绝
     * @param paramMap
     * @return
	 * @throws ParseException 
     */
	@Override
	public void updateRedEnvelope(Map<String, Object> paramMap) throws ParseException {
		String redEnvelopeStatus=(String)paramMap.get("redEnvelopeStatus");
		 Date now=new Date();
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		 List<JSONObject>  jsonList=(List<JSONObject>)paramMap.get("jsonList");
		for (JSONObject json : jsonList) {
			RedEnvelope redEnvelope=new RedEnvelope();
		   Date startDate=sdf.parse(((String) json.get("startDate")).substring(0, 10));
			if("ALLREADY_AUDIT".equals(redEnvelopeStatus)){
			if(startDate.compareTo(now)<=0){
				redEnvelope.setStatus("NO_USE");
			}else{
				redEnvelope.setStatus("WAIT_VALID");
			}
		  }else if("REJECT".equals(redEnvelopeStatus)){
			  redEnvelope.setStatus("REJECT");
			}	
			 redEnvelope.setExchangeNo((String)json.get("exchangeNo"));
			redEnvelopeDao.updateRedEnvelope(redEnvelope);
		}
		
		
	}

	
	/**
     * 红包批量插入
     * @param List<JSONObject>
     * @return
     */
	@Override
	public void insertRedEnvelopeBatch(List<JSONObject> paramList)
			throws ParseException {
		String exchangeNo=redEnvelopeDao.getMaxExchangeNo();
		Integer exchangeNo2= Integer.parseInt(exchangeNo.substring(2));
		for (JSONObject json : paramList) {
			Map<String,Object> paramMap=JsonToHashMap.toHashMap(json);
			StringBuffer newExchangeNo=new StringBuffer("HB");
			StringBuffer newAvailActions=new StringBuffer();
			exchangeNo2=exchangeNo2+1;
			int exchangeNoLength=exchangeNo2.toString().length();
			for(int i=0;i<8-exchangeNoLength;i++){
				newExchangeNo.append("0");
			}
			String availPlatform=(String)paramMap.get("availPlatform");
			if(availPlatform.trim().indexOf("全部")>=0){
				availPlatform=	availPlatform.replace("全部", "ALL");
				
			}
		    String[] availActionsAttr=((String)paramMap.get("availActions")).split(",");
		    for(int i=0;i<availActionsAttr.length;i++){
		    	if("直投".equals(availActionsAttr[i])){
		    		newAvailActions.append("D,");
		    	}
		    	if("计划".equals(availActionsAttr[i])){
		    		newAvailActions.append("P,");
		    	}
		    	if("债转".equals(availActionsAttr[i])){
		    		newAvailActions.append("T,");
		    	}
		    	if("借抵".equals(availActionsAttr[i])){
		    		newAvailActions.append("W,");
		    	}
		    	if("兑现".equals(availActionsAttr[i])){
		    		newAvailActions.append("R,");
		    	}
		}
		    if(newAvailActions!=null){
		    	
		    	newAvailActions=new StringBuffer(newAvailActions.substring(0, newAvailActions.length()-1));	
		    }
			 newExchangeNo.append(exchangeNo2.toString());
			 paramMap.put("availActions", newAvailActions.toString());
			 paramMap.put("availPlatform", availPlatform);
			paramMap.put("exchangeNo", newExchangeNo.toString());
			redEnvelopeDao.insertRedEnvelopeBatch(paramMap);
		}
	}

	@Override
	public List<String> searchAllCustName() {
		
		return redEnvelopeDao.searchAllCustName();
	}

}
